﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryTicket : ADONetRepositoryBase<Ticket> 
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Ticket entity)
        {
            var cmd = new SqlCommand("TicketInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("TicketDateTime", entity.TicketDateTime));
            cmd.Parameters.Add(new SqlParameter("Payment", entity.Payment));
            cmd.Parameters.Add(new SqlParameter("PatientId", entity.Patient.Id));
            cmd.Parameters.Add(new SqlParameter("MedServiceId", entity.MedService.Id));
            cmd.Parameters.Add(new SqlParameter("DoctorId", entity.Doctor.Id));
            logger.Trace("SqlCommand для записи данных Талона в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("TicketDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Талона id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Ticket entity)
        {
            var cmd = new SqlCommand("TicketUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("TicketDateTime", entity.TicketDateTime));
            cmd.Parameters.Add(new SqlParameter("Payment", entity.Payment));
            cmd.Parameters.Add(new SqlParameter("PatientId", entity.Patient.Id));
            cmd.Parameters.Add(new SqlParameter("MedServiceId", entity.MedService.Id));
            cmd.Parameters.Add(new SqlParameter("DoctorId", entity.Doctor.Id));
            logger.Trace("SqlCommand для обновления данных Талона id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetTicket", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Талона id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetTicketByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Талона {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Ticket GetEntity(SqlDataReader sqlDataReader)
        {
            Ticket ticket = new Ticket();
            IRepository<Patient> adoNetRepositoryPatient = new ADONetRepositoryPatient();
            IRepository<MedService> adoNetRepositoryMedService = new ADONetRepositoryMedService();
            IRepository<Doctor> adoNetRepositoryDoctor = new ADONetRepositoryDoctor();
            ticket.Id = (int)sqlDataReader["Id"];
            ticket.TicketDateTime = (DateTime)sqlDataReader["TicketDateTime"];
            ticket.Payment = sqlDataReader["Payment"].ToString();
            ticket.Patient = adoNetRepositoryPatient.GetById((int)sqlDataReader["PatientId"]);
            ticket.MedService = adoNetRepositoryMedService.GetById((int)sqlDataReader["MedServiceId"]);
            ticket.Doctor = adoNetRepositoryDoctor.GetById((int)sqlDataReader["DoctorId"]);
            return ticket;
        }
    }
}
